package com.eascs.placeonfile.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.common.service.IPlaceOnFileService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.placeonfile.service.O2oFileService;
import com.eascs.workflow.common.entity.wf.ProcessTemplate;
import com.eascs.workflow.common.service.wf.WorkFlowService;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFRequestParam;

@Service
public class O2oPlaceOnFileServiceImpl implements IPlaceOnFileService {
	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	O2oFileService fileService;
	@Reference
	WorkFlowService workFlowService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;

	@Override
	public com.eascs.admin.common.vo.ModelResult<Boolean> startUpPlaceOnFile() {
		logger.debug("自动启动归档，调用成功");
		com.eascs.admin.common.vo.ModelResult<Boolean> modle = new com.eascs.admin.common.vo.ModelResult<>();
		try {
			List<O2oLoanapplyEntity> loans = fileService.getLoans();
			if (null != loans && loans.size() > 0) {
				for (int i = 0; i < loans.size(); i++) {
					this.startUp(loans.get(i).getLoanapplyNo());
				}
				return modle.withModel(Boolean.TRUE);
			} else {
				logger.debug("自动启动归档，调用成功,但没有需要启动的单。");
				return modle.withModel(Boolean.TRUE);
			}
		} catch (Exception e) {
			logger.error("归档流程启动失败，失败原因：{}", e.getMessage());
			return modle.withError("no order for startup place on file", e.getMessage());
		}
	}

	/**
	 * @Title: startUp
	 * @Description: 自动启动归档
	 * @author 
	 * @param loanapplyNo
	 * @return
	 * @return ModelResult<Boolean>
	 * @throws
	 * @date 2017年3月16日 上午10:09:13
	 */
	public ModelResult<Boolean> startUp(String loanapplyNo) {
		try {
			O2oLoanapplyEntity loan = o2oLoanapplyService.findById(loanapplyNo);
			WFRequestParam param = new WFRequestParam();
			param.setReferId(loan.getLoanapplyNo());
			param.setReferCode(loan.getBillCode());
			param.setReferUrl("http://xxxx.eascs.com/getDetail");
			param.setSystemModule(ProcessTemplate.SYSTEM_MODULE_O2O);
			param.setProcessType(String.valueOf(ProcessTemplate.PROECESS_TYPE_PLACE_ON_FILE));
			param.setProcessVersion("v1");
			param.setAppliedby(loan.getOperatorNo());
			param.setAppliedbyName(loan.getOperatorName());
			param.setOrgNo(loan.getLtd());
			logger.debug("启动归档，参数有贷款id：【{}】，贷款编号：【{}】，系统模块：【{}】，流程类型：【{}】，经营平台id：【{}】", loan.getLoanapplyNo(), loan.getBillCode(), ProcessTemplate.SYSTEM_MODULE_O2O, param.getProcessType(), loan.getLtd());
			ModelResult<Boolean> modelResult = workFlowService.instance(param);
			if (modelResult.isSuccess()) {
				o2oLoanapplyService.updateOnfileStatus(StatusContexts.WFTYPE_STATUS_RUN, loan.getLoanapplyNo());
			} else {
				logger.error("启动归档失败，贷款单号为：【{}】，错误信息{}", loanapplyNo, modelResult.getErrMsg());
			}
			return modelResult;
		} catch (Exception e) {
			logger.error("归档失败，贷款单号为：【{}】，错误：{}，", loanapplyNo, e.getMessage());
			return new ModelResult<Boolean>("place on file error", "归档失败");
		}
	}
}
